<template>
	<view class="page">
        <view class="cellGroup bg-fff">
            <view class="cellItem flex" @click="handleQuitAccount">
                <text>退出登录</text>
                <up-icon name="arrow-right" color="#333333" size="14"></up-icon>
            </view>

            <view class="cellItem flex" @click="handleCancellation">
                <text>注销账号</text>
                <up-icon name="arrow-right" color="#333333" size="14"></up-icon>
            </view>

            <view class="cellItem flex" @click="authSetting">
                <text>权限设置</text>
                <up-icon name="arrow-right" color="#333333" size="14"></up-icon>
            </view>
        </view>
	</view>
</template>
<script>
    import { mapState, mapActions } from 'pinia';
    import { useUserInfoStore, useIndexStore } from '@/store/index';
    import { checkIsLogin, clearToken } from '@/common/common';
    import { getUserInfo } from '@/common/api/user';
    import request from '@/common/vmeitime-http';
	import { quitAccountAPI, cancellationAPI } from '@/common/api/my';
    
	export default {
		data() {
			return {
			}
		},
        computed:{
            ...mapState(useUserInfoStore, ['userInfo']),
        },
		methods: {
            ...mapActions(useUserInfoStore, ['clearUserinfo']),
            ...mapActions(useIndexStore, ['setTabIndex']),
            
			//退出登录
			handleQuitAccount(){
                const that = this;
                if(!checkIsLogin()) {
                    uni.$u.toast('当前处于未登录状态');
                    return
                }
                uni.showModal({
                	title: '提示',
                	content: '是否确认退出当前账号登录?',
                	success: async function (res) {
                		if (res.confirm) {
                            await quitAccountAPI();
                            await clearToken();
                            await request.getToken();
                            let userInfoRes = await getUserInfo();
                            if(userInfoRes.status == 0){
                                that.clearUserinfo(userInfoRes.detail);
                                uni.setStorage({
                                    key: 'userInfo',
                                    data: userInfoRes.detail
                                })
                            }
                            that.setTabIndex(0);
                            uni.switchTab({
                                url: '/pages/tabBar/home/index'
                            });
                		}
                	}
                });
			},
			
			//注销账号
			handleCancellation(){
                const that = this;
                if(!checkIsLogin()) {
                    uni.$u.toast('当前处于未登录状态');
                    return
                }
                uni.showModal({
                	title: '提示',
                	content: '注销后账号内的信息会被删除或匿名化处理，不可找回',
                	success: async function (res) {
                		if (res.confirm) {
                            await cancellationAPI();
                            await clearToken();
                            await request.getToken();
                            let userInfoRes = await getUserInfo();
                            if(userInfoRes.status == 0){
                                that.clearUserinfo(userInfoRes.detail);
                                uni.setStorage({
                                    key: 'userInfo',
                                    data: userInfoRes.detail
                                })
                            }
                            that.setTabIndex(0);
                            uni.switchTab({
                                url: '/pages/tabBar/home/index'
                            });
                		}
                	}
                });
			},
            
            //权限设置
            authSetting() {
                uni.openSetting()
            }
		}
	}
</script>
<style lang="scss" scoped>
.page{
    min-height: 100vh;
    .cellGroup{
        padding: 28rpx;
        .cellItem{
            padding: 20rpx 0;
            justify-content: space-between;
        }
    }
}
</style>
